package com.party.core.service.collect.biz;

import com.party.common.utils.StringUtils;
import com.party.core.model.collect.SystemModuleCollect;
import com.party.core.model.collect.SystemPlatformCollect;
import com.party.core.redisDao.collect.ModuleCollectRedisDao;
import com.party.core.redisDao.collect.PlatformCollectRedisDao;
import com.party.core.service.collect.ISystemModuleCollectService;
import com.party.core.service.collect.ISystemPlatformCollectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: yangshoukun
 * @Date: 2018/6/21 15:44
 */
@Service
public class SystemCollectBizService {
    @Autowired
    private ISystemPlatformCollectService systemPlatformCollectService;

    @Autowired
    private ISystemModuleCollectService systemModuleCollectService;

    @Autowired
    private ModuleCollectRedisDao moduleCollectRedisDao;

    @Autowired
    private PlatformCollectRedisDao platformCollectRedisDao;

    private final String KEY_MODULE = "visit_module_key_";

    private final String KEY_PLATFORM = "visit_platform_key";

    private final long DEFAULT_TIME = 2 * 60 * 60;

    private final int DEFAULT_LIMIT = 1000;

    public void countModule(SystemModuleCollect param) {
        if (null == param) {
            return;
        }
        if (StringUtils.isEmpty(param.getModule()) || null == param.getCreateDate()) {
            return;
        }
        saveModule(param);
    }

    public void countPlatform(SystemPlatformCollect param) {
        if (null == param) {
            return;
        }
        if (null == param.getCreateDate()) {
            return;
        }
        savePlatform(param);
    }

    private void saveModule(SystemModuleCollect module) {
        final String key = KEY_MODULE + module.getModule();
        SystemModuleCollect redisModule = moduleCollectRedisDao.getValue(key);
        if (null == redisModule) {
            moduleCollectRedisDao.setValue(key, module, DEFAULT_TIME);
        } else {
            redisModule.setUvNum((redisModule.getUvNum() == null ? 0 : redisModule.getUvNum())
                    + (module.getUvNum() == null ? 0 : module.getUvNum()));
            redisModule.setPvNum((redisModule.getPvNum() == null ? 0 : redisModule.getPvNum())
                    + (module.getPvNum() == null ? 0 : module.getPvNum()));
            redisModule.setUpdateDate(module.getCreateDate());
            moduleCollectRedisDao.delete(key);
            moduleCollectRedisDao.setValue(key, redisModule);
        }

        // 判断如果统计数量超过default_limit就统一写入数据库
        addOrNotModule(true, module.getModule());
    }

    public void addOrNotModule(boolean judgeTime, String moduleCode) {
        final String key = KEY_MODULE + moduleCode;
        SystemModuleCollect redisModule = moduleCollectRedisDao.getValue(key);
        if (null == redisModule) {
            return;
        }
        if (judgeTime && redisModule.getPvNum() < DEFAULT_LIMIT) {
            return;
        }
        insertOrUpdateModule(redisModule);
        moduleCollectRedisDao.delete(key);
    }

    private void insertOrUpdateModule(SystemModuleCollect param) {
        SystemModuleCollect moduleCollect = systemModuleCollectService.findByModuleAndDate(param.getModule(), param.getCreateDate());
        if (null == moduleCollect) {
            moduleCollect = new SystemModuleCollect();
            BeanUtils.copyProperties(param, moduleCollect);
            if (null == moduleCollect.getPvNum()) {
                moduleCollect.setPvNum(0L);
            }
            if (null == moduleCollect.getUvNum()) {
                moduleCollect.setUvNum(0L);
            }
            systemModuleCollectService.insert(moduleCollect);
        } else {
            moduleCollect.setUvNum((moduleCollect.getUvNum() == null ? 0 : moduleCollect.getUvNum())
                    + (param.getUvNum() == null ? 0 : param.getUvNum()));
            moduleCollect.setPvNum((null == moduleCollect.getPvNum() ? 0 : moduleCollect.getPvNum())
                    + (param.getPvNum() == null ? 0 : param.getPvNum()));
            systemModuleCollectService.update(moduleCollect);
        }
    }

    private void savePlatform(SystemPlatformCollect platform) {
        final String key = KEY_PLATFORM;
        SystemPlatformCollect redisPlatform = platformCollectRedisDao.getValue(key);
        if (null == redisPlatform) {
            platformCollectRedisDao.setValue(key, platform, DEFAULT_TIME);
        } else {
            redisPlatform.setUvNum((redisPlatform.getUvNum() == null ? 0 : redisPlatform.getUvNum() )
                    + (platform.getUvNum() == null ? 0 : platform.getUvNum()));
            redisPlatform.setPvNum((redisPlatform.getPvNum() == null ? 0 : redisPlatform.getPvNum())
                    + (platform.getPvNum() == null ? 0 : platform.getPvNum()));
            redisPlatform.setUpdateDate(platform.getCreateDate());
            platformCollectRedisDao.delete(key);
            platformCollectRedisDao.setValue(key, redisPlatform);
        }

        // 判断如果统计数量超过default_limit就统一写入数据库
        addOrNotPlatform(true);
    }

    public void addOrNotPlatform(boolean judgeTime) {
        final String key = KEY_PLATFORM;
        SystemPlatformCollect redisPlatform = platformCollectRedisDao.getValue(key);
        if (null == redisPlatform) {
            return;
        }
        if (judgeTime && redisPlatform.getPvNum() < DEFAULT_LIMIT) {
            return;
        }
        insertOrUpdatePlatform(redisPlatform);
        platformCollectRedisDao.delete(key);
    }

    private void insertOrUpdatePlatform(SystemPlatformCollect param) {
        SystemPlatformCollect platformCollect = systemPlatformCollectService.findByCreateDate(param.getCreateDate());
        if (null == platformCollect) {
            platformCollect = new SystemPlatformCollect();
            BeanUtils.copyProperties(param, platformCollect);
            if (null == platformCollect.getPvNum()) {
                platformCollect.setPvNum(0L);
            }
            if (null == platformCollect.getUvNum()) {
                platformCollect.setUvNum(0L);
            }
            systemPlatformCollectService.insert(platformCollect);
        } else {
            platformCollect.setUvNum((platformCollect.getUvNum() == null ? 0 : platformCollect.getUvNum())
                    + (param.getUvNum() == null ? 0 : param.getUvNum()));
            platformCollect.setPvNum((platformCollect.getPvNum() == null ? 0 : platformCollect.getPvNum())
                    + (param.getPvNum() == null ? 0 : param.getPvNum()));
            systemPlatformCollectService.update(platformCollect);
        }
    }
}
